package com.gdit.zax.servletjsp;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.alibaba.fastjson.JSONArray;
import com.gdit.zax.Constant;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.FromRequest;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.WebUpload;
import com.gdit.zax.utils.XlsUtil;

public class addclass extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FromRequest fr = new FromRequest(getServletContext(), request, response);
		FileItem xlsfile = (FileItem) fr.getParameter("xlsfile");
		if (xlsfile == null || "".equals(xlsfile.getName())) {//选择了图片
			Repost.message("请选择excel文件!", request, response);
			return;
		}

		File xfile = new File(xlsfile.getName());
		
		String exname = WebUpload.getExtensionName(xlsfile.getName());
		if (exname == null || !exname.equals("xls")) {
			Repost.message("请选择97-2003的excel文件!", request, response);
			return;
		}
		
		if (!WebUpload.save(getServletContext(), xlsfile, "upload", xfile.getName())){
			Repost.message("上传文件失败，请重试!", request, response);
			return;
		}
		
		String schoolname = xfile.getName().substring(0, xfile.getName().length()-4);
		
		if (schoolname.indexOf("新建")!=-1) {
			Repost.message("文件名可能不是学校名，请重试!", request, response);
			return;
		}
		
		Connection conn = DBUtil.getConnection();
		
		//处理
		if (conn == null) {
			Repost.dberror(request, response);
			return;
		}
		
		DAOFactory dao = new DAOFactory(conn, request, response);
		
		
		xfile = new File(Constant.WEBPATH + File.separator + "upload",xfile.getName());
		UserDao udao = dao.getUserDao();
		ArrayList<UserInfo> list = XlsUtil.getAddUserInfo(dao,xfile);
		LogUtil.print("xls结果:" + JSONArray.toJSONString(list));
		
		if (list==null) {
			Repost.message("系统出错，请重试!", request, response);
			return;
		}
		if (udao.getRegisterId()!= list.get(0).getId()) {
			Repost.message("操作已超时，请重试!", request, response);
			return;
		}
		LogUtil.print("xls:" + xfile.getAbsolutePath() + " 学生数:" + list.size());
		for (UserInfo userInfo : list) {
			if (!udao.isRegister(userInfo.getSchoolid(), userInfo.getUsername())) {
				LogUtil.print("注册用户:" + userInfo.getUsername());
				udao.addUser(userInfo);//给他们注册
			}
		}
		
		
		Repost.message("所有学生注册已成功!", request, response);
		
	}

}
